<template>
	<view class="">
		<!--  -->
		<ben></ben>
		<!--  -->
		<u-swiper :list="swiperlist" height="280" imgMode="scaleToFill" radius="0" @click="previewImage"></u-swiper>
		<view class="min30">
			<view class="background_f rad2 pad20 minbottom30">
				<view class="col3 size30 font600 omit2">
					{{ info.title }}
				</view>
				<view class="mintop20 col9 size30 font500">
					规格：{{ specs_list[specsIndex].name }}
				</view>
				<view class="mintop20 col9 size30 font500 row align_item_c">
					月销99
					<view class="minau minright0 size24 font500 row" style="color: #FE3C3C;align-items: baseline;">
						￥
						<view class="font600 size36">
							{{ specs_list[specsIndex].selling_price }}
						</view>
					</view>
				</view>
			</view>
			<view class="background_f row rad2 pad20 minbottom30 align_item_c">
				<view class="size30 col3 font600">
					选择规格
				</view>
				<view class="minau minright0 row" style="align-items: flex-end;" @click="show = true">
					<view class="size24 col9 font500">
						共{{ specs_list.length }}种规格可选
					</view>
					<u-icon name="arrow-right" color="#999" size="14"></u-icon>
				</view>
			</view>
			<!-- <view class="background_f row rad2 pad20 minbottom30 align_item_c">
				<view class="size30 col3 font600">
					选择数量
				</view>
				<view class="size22 font500 minleft10" style="color: #FE3C3C;">
					限购6件
				</view>
				<view class="minau minright0">
					<u-number-box v-model="goods_num" @change="valChange"></u-number-box>
				</view>
			</view> -->
			<view class="background_f rad2" style="padding: 30rpx;">
				<view class="row">
					<image :src="info.shop.stores_logo" mode="" class="rad1" style="width: 100rpx;height: 100rpx;">
					</image>
					<view class="column flex1 minleft20">
						<view class="col3 font600 size28 row align_item_c">
							<view class="flex1">
								{{ info.shop.stores_name }}
							</view>
							<view class="minau minright0 size24 col9 font500 row " style="align-items: flex-end;"
								@click="$jump.nav('/indexpackage/user/store_info?stores_id=' + info.shop.id)">
								进入店铺
								<u-icon name="arrow-right" color="#999" size="14"></u-icon>
							</view>
						</view>
						<view class="minto minbottom0 size26 col6 font500">
							地址：{{ info.shop.stores_address }}
						</view>
					</view>
				</view>
			</view>
			<view class="text_center font500 size32 col0 min30_tb">
				商品详情
			</view>
			<view class="">
				<mp-html :content="info.product_detail" />
			</view>
		</view>
		<view style="height: 80px;">

		</view>
		<u-popup :show="show" @close="show = false" closeable="true" :close-on-click-overlay="false">
			<view class="background_f column" style="padding: 20rpx 30rpx; margin-bottom: 65px;">
				<view class="minauto mintop10 col0 size30 font600 text_center">
					选择规格
				</view>
				<view class="column mintop30">
					<view class="size30 col3 font600">
						选择规格
					</view>
					<view class="mintop20 row flexwrap">
						<view class="rad1 specs size26 font500"
							:style="specsIndex == specs_index ? 'color: #EC1F1F;' : 'color:#333;'"
							v-for="(specs_item,specs_index) in specs_list" :key="specs_index"
							@click="tabSpecs(specs_index)">
							{{ specs_item.name }}
						</view>
					</view>
				</view>
				<view class="column min30_tb">
					<view class="size30 col3 font600">
						选择数量
					</view>
					<view class="mintop20">
						<u-number-box v-model="goods_num"></u-number-box>
					</view>
				</view>
			</view>
		</u-popup>
		<!-- 底部 -->
		<view class="background_f posfix row" style="bottom: 0;padding: 20rpx 30rpx;z-index: 10076;height: 55px;"
			:style="{ width: screenWidth + 'px' }">
			<view class="column">
				<view class="row size24 font500" style="color: #EC1F1F;align-items: baseline;">
					￥
					<view class="size36 font600">
						{{ total.totalPrice }}
					</view>
				</view>
				<view class="size24 font500 mintop10" style="color: #979797;">
					购买可得{{ total.totalIntegral }}积分
				</view>
			</view>
			<view class="minau minright0 row" v-if="show == true">
				<!-- <view class="minauto" style="width: 160rpx;">
					<u-button text="加入购物车" shape="circle" color="linear-gradient( 319deg, #FF612F 4%, #FFB800 100%)"
						size="small" @click="getAddcar"></u-button>
				</view> -->
				<view class="minauto minleft40" style="width: 160rpx;">
					<u-button text="立即购买" shape="circle" color="linear-gradient( 319deg, #FF612F 4%, #FFB800 100%)"
						size="small" @click="onNav"></u-button>
				</view>
			</view>
			<view class="minau minright0 row" v-else>
				<!-- <view class="minauto" style="width: 160rpx;">
					<u-button text="加入购物车" shape="circle" color="linear-gradient( 319deg, #FF612F 4%, #FFB800 100%)"
						size="small" @click="show = true"></u-button>
				</view> -->
				<view class="minauto minleft40" style="width: 160rpx;">
					<u-button text="立即购买" shape="circle" color="linear-gradient( 319deg, #FF612F 4%, #FFB800 100%)"
						size="small" @click="show = true"></u-button>
				</view>
			</view>
		</view>
		<!-- 底部 -->
	</view>
</template>

<script>
	import Decimal from 'decimal.js';
	export default {
		data() {
			return {
				totalPrice: '',
				totalIntegral: '',
				screenWidth: '',
				user_id: '',
				show: false,
				specsIndex: 0,
				info: [],
				swiperlist: [],
				goods_num: 1,
				specs_list: [],
			}
		},

		onLoad(op) {
			this.id = op.id
			let app = uni.getSystemInfoSync()
			this.screenWidth = app.screenWidth - 30
		},
		onShow() {
			//当前页面展示的时候他就执行该生命周期
			const token = uni.getStorageSync('token');
			this.user_id = uni.getStorageSync('user_id');
			if (token) {
				//如果存在token
				this.getGoodsInfo()
				//按正常逻辑来讲 登录成功 我们是不是需要向服务器获取用户的信息？
				//那么我们一起来获取用户信息
			} else {

				uni.showToast({
					title: '您还未登录，正在前往登录',
					icon: 'none'
				})
				setTimeout(function() {
					uni.navigateTo({
						url: "/mypackage/user/login"
					})
				}, 1000)
			}
		},
		onHide() {
			this.show = false
		},
		computed: {
			total() {
				let Integral = new Decimal(0);
				let prices = new Decimal(0);
				let totalIntegral = 0
				if (this.specs_list && this.specs_list[this.specsIndex]) {
					prices = new Decimal(this.specs_list[this.specsIndex].selling_price).mul(Decimal(this.goods_num))
					Integral = new Decimal(this.specs_list[this.specsIndex].sore).mul(Decimal(prices))
					const a = 100
					totalIntegral = new Decimal(Integral).div(new Decimal(a))
				}
				return {
					totalPrice: prices.toFixed(2),
					totalIntegral: totalIntegral.toFixed(0),
				}
			}
		},
		methods: {
			previewImage(index) {
				console.log(index);
				uni.previewImage({
					current: index, // 当前显示图片索引
					urls: this.swiperlist // 需要预览的图片http链接列表
				});
			},
			async getGoodsInfo() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("home/goods/" + this.id)
				if (code == 200) {
					this.info = data
					this.swiperlist = data.img
					this.specs_list = data.specs
				} else {
					console.log(message);
				}
			},
			async getAddcar() {
				let {
					code,
					message,
					data
				} = await this.$com.fetch("car/car_goods/" + this.user_id, {
					product_id: this.id,
					specs_id: this.specs_list[this.specsIndex].id,
					shop_id: this.info.shop_id,
					specs_num: this.goods_num,
				})
				if (code == 200) {
					this.show = false
					uni.showToast({
						icon: 'success',
						title: message
					})
				} else {
					this.show = false
					uni.showToast({
						icon: 'success',
						title: message
					})
				}
			},
			onNav() {
				var all = []
				let cartarr = {}
				let arr = {}
				cartarr["stores_name"] = this.info.shop.stores_name
				cartarr["stores_logo"] = this.info.shop.stores_logo
				cartarr["stores_id"] = this.info.shop.id
				cartarr["products"] = []
				arr["car_id"] = ''
				arr["id"] = this.info.id
				arr["image"] = this.info.image
				arr["title"] = this.info.title

				this.specs_list[this.specsIndex].specs_num = this.goods_num
				arr["specs"] = this.specs_list[this.specsIndex]
				cartarr["products"].push(arr)
				all.push(cartarr)
				console.log(all);
				uni.navigateTo({
					url: '/indexpackage/user/place_order?list=' + JSON.stringify(all)
				});
			},
			tabSpecs(i) {
				this.specsIndex = i
			},
		}
	}
</script>

<style scoped lang="less">
	.specs {
		background-color: #F8F9FA;
		padding: 6rpx 16rpx;
		margin-left: 10rpx;

		&:nth-child(1) {
			margin-left: 0;
		}
	}
</style>